package dshop.model;

import com.jfinal.plugin.activerecord.Db;

import appbase.exception.BizzException;
import appbase.jfinal.ext.BaseModel;
import appbase.kit.encrypt.MD5Kit;
import appbase.utils.StringUtil;

public class User extends BaseModel<User> {

	private static final long serialVersionUID = -6967680902549117464L;

	public static final User dao = new User();

	public static final int ROLE_USER = 0;
	public static final int ROLE_ADMIN = 1;
	public static final int ROLE_SUPERADMIN = 2;

	/**
	 * @return {id, account, password, salt, userId, role, isDel}
	 */
	public static User findBy(String account, int role) {
		String sql = "select id, account, password, salt, userId, role, isDel , address,user from user where account = ?";
		return dao.findFirst(sql, account);
	}

	/**
	 * @return {userId, account}
	 */
	public static User getOf(int userId, int role) {
		return dao.findFirstByColumns("userId, account", "userId, role", userId, role);
	}

	public static User create(String account, String plainTextPassword, int role) {
		User user = new User();
		user.set("account", account).set("password", MD5Kit.encrptMD5(plainTextPassword)).set("role", role);
		return user;
	}

	/**
	 * @throws BizzException ("帐号已存在")
	 */
	public static int regist(String account, String md5Password, int role, String address, String parent, String userp)
			throws BizzException {
		if (isExist(account, role)) {
			throw new BizzException("帐号【" + account + "】已存在");
		}

		String salt = generateSalt();
		String encryptPass = encryptPass(md5Password, salt);

		User user = new User();
		user.set("password", encryptPass).set("salt", salt).set("account", account).set("role", role)
				.set("address", address).set("user", userp).save();
		return user.getInt("id");
	}

	public static void setUserId(int id, int userId) {
		dao.updateColumnBy("userId", "id", userId, id);
	}

	/**
	 * 修改密码
	 */
	public static void modifyPass(int userId, int role, String oldPassMd5, String newPassMd5, String confirmPassMd5)
			throws Exception {
		if (oldPassMd5 != null && oldPassMd5.equals(newPassMd5)) {
			throw new Exception("新密码不能和旧密码一样");
		}
		if (confirmPassMd5 == null || !confirmPassMd5.equals(newPassMd5)) {
			throw new Exception("新密码和确认密码不一样");
		}

		User user = dao.findFirstByColumns("password, salt", "id, role", userId, role);

		String encrptOldPass = "";
		if (user != null) {
			encrptOldPass = encryptPass(oldPassMd5, user.getStr("salt"));
		}

		if (user != null && oldPassMd5 != null && newPassMd5 != null
				&& user.getStr("password").equalsIgnoreCase(encrptOldPass)) {
			modifyPass(userId, role, newPassMd5);
			return;
		}
		throw new Exception("旧密码错误");
	}

	/**
	 * 修改密码
	 */
	public static void modifyPass(int userId, int role, String passwordMd5) {
		String salt = generateSalt();
		String encryptPass = encryptPass(passwordMd5, salt);
		String sql = "update user set password = ?, salt = ? where id = ? and role = ?";
		Db.update(sql, encryptPass, salt, userId, role);
	}

	/**
	 * 检查用户名是否存在
	 */
	public static boolean isExist(String account, int role) {
		return dao.exist("account, role", account, role);
	}

	public static boolean checkPass(String passwordA, String passwordB) {
		return passwordA != null && !passwordA.isEmpty() && passwordA.equalsIgnoreCase(passwordB);
	}

	public static String generateSalt() {
		return StringUtil.randStr(6);
	}

	public static String encryptPass(String md5Password, String salt) {
		return MD5Kit.encrptMD5(md5Password.toLowerCase() + salt).toLowerCase();
	}

	/**
	 * System.currentTimeMillis() + StringUtil.randStr(6)
	 */
	public static String createPlainTextPassword() {
		return System.currentTimeMillis() + StringUtil.randStr(6);
	}

	public static void delete(int userId, int role) {
		String sql = "update user set isDel = 1 where id = ? and role = ?";
		Db.update(sql, userId, role);
	}

	public static void recover(int userId, int role) {
		String sql = "update user set isDel = 0 where id = ? and role = ?";
		Db.update(sql, userId, role);
	}
}
